//
//  ImaOnRightBtn.m
//  神约
//
//  Created by qlwl on 17/1/22.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "ImaOnRightBtn.h"
#import "UIView+FrameKIt.h"
#import "NSString+Length.h"

@implementation ImaOnRightBtn

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    if (self.titByteLenght !=0 ) {
        if ([title textLength] > self.titByteLenght ) {
            title = [[title subStringToIndexByte:self.titByteLenght - 2] stringByAppendingString:@"..."];
        }
    }
    [super setTitle:title forState:state];
}
- (void)setTitByteLenght:(NSUInteger)titByteLenght
{
    if (_titByteLenght != titByteLenght) {
        _titByteLenght = titByteLenght;
        [self setTitle:self.titleLabel.text forState:UIControlStateNormal];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self sizeToFit];
    
    // 还可增设间距
    CGFloat spacing = 0;
    // 图片右移
    CGSize imageSize = self.imageView.frame.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width * 2 - spacing, 0.0, 0.0);
    // 文字左移
    CGSize titleSize = self.titleLabel.frame.size;
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, - titleSize.width * 2 - spacing);
}

@end
